home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / mint / lib / mntlib44.zoo / mntlib / fopen_i.c < prev    next >
C/C++ Source or Header  |  1994-03-01  |  1KB  |  80 lines

  1. /* from Dale Schumacher's dLibs */
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <fcntl.h>
  6. #include <unistd.h>
  7. #include "lib.h"
  8.  
  9. extern int __mint;
  10.  
  11. extern int __default_mode__;
  12.  
  13. FILE *
  14. _fopen_i(filename, mode, fp)
  15.     const char *filename;
  16.     const char *mode;
  17.     FILE *fp;
  18. /*
  19.  *    INTERNAL FUNCTION.  Attempt to open <filename> in the given
  20.  *    <mode> and attach it to the stream <fp>
  21.  */
  22.     {
  23.     register int h, i, iomode = 0, f = __default_mode__;
  24.  
  25.     while (*mode)
  26.         {
  27.         switch (*mode++)
  28.             {
  29.             case 'r':
  30.                 f |= _IOREAD;
  31.                 break;
  32.             case 'w':
  33.                 f |= _IOWRT;
  34.                 iomode |= (O_CREAT | O_TRUNC);
  35.                 break;
  36.             case 'a':
  37.                 f |= _IOWRT;
  38.                 iomode |= (O_CREAT | O_APPEND);
  39.                 break;
  40.             case '+':
  41.                 f &= ~(_IOREAD | _IOWRT);
  42.                 f |= _IORW;
  43.                 break;
  44.             case 'b':
  45.                 f |= _IOBIN;
  46.                 break;
  47.             case 't':
  48.                 f &= ~_IOBIN;
  49.                 break;
  50.             default:
  51.                 return(NULL);
  52.             }
  53.         }
  54.     if ((i = (f & (_IORW | _IOREAD | _IOWRT))) == 0)
  55.         return(NULL);
  56.     else if (i == _IOREAD)
  57.         iomode |= O_RDONLY;
  58.     else if (i == _IOWRT)
  59.         iomode |= O_WRONLY;
  60.     else
  61.         iomode |= O_RDWR;
  62.     iomode |= O_NOCTTY;
  63.     h = open(filename, iomode, 0666);
  64.     if (h < __SMALLEST_VALID_HANDLE)
  65.         {
  66.         return(NULL);        /* file open/create error */
  67.         }
  68.     if (isatty(h))
  69.         f |= __mint ? (_IODEV | _IONBF| _IOBIN) : (_IODEV | _IONBF);
  70.     else
  71.         f |= _IOFBF;
  72.     fp->_file = h;            /* file handle */
  73.     fp->_flag = f;            /* file status flags */
  74.     if (iomode & O_APPEND)
  75.         (void) fseek(fp, 0L, SEEK_END);
  76.  
  77.     return (fp);
  78.     }
  79.  
  80.